package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FDayExpDetDS {

	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;

	
	public ASM_FDayExpDetDS (Context context)
	{
		this.context = context;
		dbHelper = new ASM_DB(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public int Create(ASM_FDayExpDet det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		 
	
		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmDExpDet_RefNo, det.getAsmDExpDet_RefNo());
		values.put(ASM_DB.asmDExpDet_TxnDate, det.getAsmDExpDet_TxnDate());
		values.put(ASM_DB.asmDExpDet_SeqNo, det.getAsmDExpDet_SeqNo());
		values.put(ASM_DB.asmDExpDet_ExpCode, det.getAsmDExpDet_ExpCode());
		values.put(ASM_DB.asmDExpDet_ExpName, det.getAsmDExpDet_ExpName());
		values.put(ASM_DB.asmDExpDet_Amt, det.getAsmDExpDet_Amt());
		values.put(ASM_DB.asmDExpDet_RecordId, det.getAsmDExpDet_RecordId());
		count = (int) dB.insert(ASM_DB.ASM_FDAYEXPDET, null, values);
		System.out.println(count);
		dB.close();
		return count;
		
	}
	
	
	public ArrayList<String> getExpDet(String Refno) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select amt from FDayExpDet where refno = '"+Refno+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0));
			
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getExpAllDetail(String expcode,String RefNo) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select refno,txndate,expcode,amt,ExpName from FDayExpDet where expcode = '"+expcode+"' and RefNo = '"+RefNo+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(4)+"-:-"+cursor.getString(2)+"-:-"+cursor.getString(3));
		
			
			
			
		}
		
		return Details;
	}
	
	
	public ArrayList<String> getExpAllDetailView()
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select sum(amt) from FDayExpDet group by refno ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0));
//			Details.add(cursor.getString(1));
//			Details.add(cursor.getString(2));
			
			
			
		}
		
		return Details;
	}
	
	
	public int DeleteExp(String Refno,String Expname)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FDAYEXPDET, "RefNo = '"+Refno+"' and ExpCode='"+Expname+"' ", null);
		System.out.println(count);
		dB.close();
		return count;
		
	}
	
	public int Delete(String Refno,String expCode)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FDAYEXPDET, "RefNo = '"+Refno+"' and expname = '"+expCode+"' ", null);
		System.out.println(count);
		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getfDayExpDet(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select ExpCode,Amt from FDayExpDet where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{

			Details.add(cursor.getString(0)+"~"+cursor.getString(1));
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getExpAllDetailViewEdit(String refno)
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select amt from FDayExpDet where RefNo = '"+refno+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0));
		//	Details.add(cursor.getString(1));
//			Details.add(cursor.getString(2));
			
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getExpAllDetailViewEdit1(String refno)
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select expcode from FDayExpDet where RefNo = '"+refno+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0));

			
			
			
		}
		
		return Details;
	}
	
	public int Update(ASM_FDayExpDet det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmDExpDet_RefNo,det.getAsmDExpDet_RefNo());
		values.put(ASM_DB.asmDExpDet_TxnDate,det.getAsmDExpDet_TxnDate());
		values.put(ASM_DB.asmDExpDet_SeqNo,det.getAsmDExpDet_SeqNo());
		values.put(ASM_DB.asmDExpDet_ExpCode,det.getAsmDExpDet_ExpCode());
		values.put(ASM_DB.asmDExpDet_ExpName,det.getAsmDExpDet_ExpName());
		values.put(ASM_DB.asmDExpDet_Amt,det.getAsmDExpDet_Amt());
		values.put(ASM_DB.asmDExpDet_RecordId, det.getAsmDExpDet_RecordId());
		
		String sdjf = det.getAsmDExpDet_RefNo();
		String sdjfsa = det.getAsmDExpDet_ExpCode();
		String sdjeref = det.getAsmDExpDet_ExpName();
		String serdjfsa = det.getAsmDExpDet_Amt();
		String sdwjf = det.getAsmDExpDet_RefNo();
		//String sddsjfsa = det.getAsmDExpDet_ExpCode();
	
		count = (int) dB.update(ASM_DB.ASM_FDAYEXPDET, values, "ExpCode = '"+det.getAsmDExpDet_ExpCode()+"' and RefNo = '"+det.getAsmDExpDet_RefNo()+"'", null);

		dB.close();
		return count;
		
	}
	
	public int UpdateIN(ASM_FDayExpDet det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmDExpDet_RefNo,det.getAsmDExpDet_RefNo());
		values.put(ASM_DB.asmDExpDet_TxnDate,det.getAsmDExpDet_TxnDate());
		values.put(ASM_DB.asmDExpDet_SeqNo,det.getAsmDExpDet_SeqNo());
		values.put(ASM_DB.asmDExpDet_ExpCode,det.getAsmDExpDet_ExpName());
		values.put(ASM_DB.asmDExpDet_ExpName,det.getAsmDExpDet_ExpCode());
		
		values.put(ASM_DB.asmDExpDet_Amt,det.getAsmDExpDet_Amt());
		values.put(ASM_DB.asmDExpDet_RecordId, det.getAsmDExpDet_RecordId());
		
		
	
		count = (int) dB.update(ASM_DB.ASM_FDAYEXPDET, values, "RefNo = '"+det.getAsmDExpDet_RefNo().toString()+"'  and expname = '"+det.getAsmDExpDet_ExpCode().toString()+"' ", null);

		System.out.println(det.getAsmDExpDet_RefNo());
		System.out.println(det.getAsmDExpDet_ExpCode());
		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getfDayExpDetEdit(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select * from FDayExpDet where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(1);
			String TxnDate = cursor.getString(2);
			String SeqNo = cursor.getString(3);
			String ExpCode = cursor.getString(4);
			String ExpName = cursor.getString(5);
			String Amt = cursor.getString(6);
			Details.add(RefNo+":-:"+ExpName+":-:"+Amt);

			
			
		}
		
		return Details;
	}
	
	
	public ArrayList<String> getExpAllDetailView(String refno)
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select expcode,amt from FDayExpDet where RefNo = '"+refno+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			String ExpCode = cursor.getString(0);
			String Amt = cursor.getString(1);
			Details.add(ExpCode+":-:"+Amt);
			
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getExpAllDetailHistory()
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select a.RefNo,a.TxnDate,SUM(b.Amt) from FDayExpHed as a, FDayExpDet as b where a.RefNo = b.RefNo group by a.RefNo , a.TxnDate ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			String RefNo = cursor.getString(0);
			String TxnDate = cursor.getString(1);
			String Amt = cursor.getString(2);

			Details.add(RefNo+":-:"+TxnDate+":-:"+Amt);
			
			
			
		}
		
		return Details;
	}

	public ArrayList<String> getAllasm(String expcode,String Refno) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select refno,txndate,expcode,amt,ExpName from FDayExpDet  where expcode = '"+expcode+"' and RefNo = '"+Refno+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(2));
			
			
			
		}
		
		return Details;
	}
	public int Delete(String Refno)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FDAYEXPDET, "RefNo = '"+Refno+"'", null);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getfDayExpDetfull(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select * from FDayExpDet where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(1);
			String TxnDate = cursor.getString(2);
			String SeqNo = cursor.getString(3);
			String ExpCode = cursor.getString(4);
			String ExpName = cursor.getString(5);
			String Amt = cursor.getString(6);
			Details.add(RefNo+"-:-"+ExpCode+"-:-"+ExpName+"-:-"+Amt);

			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getfDayExpdetforUpdate(String expcode,String Refno) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select * from FDayExpDet where expname = '"+expcode+"' and RefNo = '"+Refno+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(1);
			String TxnDate = cursor.getString(2);
			String SeqNo = cursor.getString(3);
			String ExpCode = cursor.getString(4);
			String ExpName = cursor.getString(5);
			String Amt = cursor.getString(6);
			Details.add(RefNo+"-:-"+ExpCode+"-:-"+ExpName+"-:-"+Amt);

			
			
		}
		
		return Details;
	}
	public String getsaveddet(String RefNo,String expcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		String Details = null;
		
	
		String selectQuery = "select * from FDayExpDet where RefNo = '"+RefNo+"' and expname = '"+expcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			Details=cursor.getString(1);
		}
		
		return Details;
	}

}
